package net.ojbk.twobox;

import net.ojbk.twobox.pojo.TcpMessage;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;

/**
 * PACKAGE_NAME: net.ojbk.twobox
 * Description:
 */
public class TCPTest7 {
    @Test
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //int localPort = 53541;
        Socket socket = new Socket();
        socket.setReuseAddress(true);
        //socket.bind(new InetSocketAddress(localPort));
        socket.connect(new InetSocketAddress("192.168.0.101", 51404));
        System.out.println("建立链接,本地地址：" + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort());

        SocketAddress sdasa =  socket.getLocalSocketAddress();
        TcpMessage tcpMessage = new TcpMessage();
        tcpMessage.code = 5;
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
        objectOutputStream.writeObject(tcpMessage);
        objectOutputStream.flush();
        // 接收
        TcpMessage responseMsg = (TcpMessage) objectInputStream.readObject();
        Object[] data = (Object[]) responseMsg.data;
        String ip = (String) data[0];
        int port = (int) data[1];
        System.out.println("外网地址：" + ip + ":" + port);
        // 断开
        //objectInputStream.close();
        //objectOutputStream.close();
        //socket.close();

        socket.connect(new InetSocketAddress("ojbk.net", 9999));

    }
}
